package com.lotustong.im4d.file;

import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;

import com.lotustong.im4d.util.Constants;

public class Images {

  // Bitmap Images
  public Image Refresh;
  Image[] AllBitmaps;
  private static Hashtable<String, Image> programIcons;

  public Images() {
  }

  public void freeAll() {
    for (int i = 0; i < AllBitmaps.length; i++)
      AllBitmaps[i].dispose();
    AllBitmaps = null;
    if (programIcons != null) {
      for (Enumeration it = programIcons.elements(); it.hasMoreElements();) {
        Image image = (Image) it.nextElement();
        image.dispose();
      }
      programIcons = null;
    }
  }

  Image createBitmapImage(Display display, String fileName) {
    Image result = null;
    try {
      InputStream sourceStream = new FileInputStream(Constants.RESOURCE_DIRECTORY_RELATIVE + fileName + ".bmp");
      ImageData source = new ImageData(sourceStream);
      ImageData mask = source.getTransparencyMask();
      result = new Image(display, source, mask);
      sourceStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return result;
  }

  Image createIconImage(Display display, String fileName) {
    Image result = null;
    try {
      InputStream sourceStream = new FileInputStream(Constants.RESOURCE_DIRECTORY_RELATIVE + fileName + ".gif");
      ImageData source = new ImageData(sourceStream);
      ImageData mask = source.getTransparencyMask();
      result = new Image(display, source, mask);
      sourceStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return result;
  }

  public void loadAll(Display display) {
    // Bitmap Images
    Refresh = createIconImage(display, "refresh");
    AllBitmaps = new Image[] { Refresh, };

    programIcons = new Hashtable<String, Image>();

    programIcons.put("icon_file", createIconImage(display, "icon_file"));
    programIcons.put("icon_folder", createIconImage(display, "icon_folder"));
    programIcons.put("icon_upfolder", createIconImage(display, "icon_upfolder"));

  }

  public static Image getIconForFileType(String name) {
    return (Image) programIcons.get(name);
  }

  public static Image getIconForFileType(Program program) {
    Image image = (Image) programIcons.get(program);
    if (image == null) {
      ImageData imageData = program.getImageData();
      if (imageData != null) {
        image = new Image(null, imageData, imageData.getTransparencyMask());
        programIcons.put(program.getName(), image);
      } else {
        image = (Image) programIcons.get("icon_file");
      }
    }
    return image;
  }
}
